package com.zhihuichongcheng.application.ui.im.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.im.NewsBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.im.adapter.NewsAdapter;
import com.zhihuichongcheng.application.ui.main.activity.MainActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 3water on 2016/10/28.
 */
public class NewsActivity extends BaseActivity {
    private XRecyclerView mRcvNews;
    private NewsAdapter mAdapter = null;
    private List<NewsBean> mListData = new ArrayList<>();
    private LinearLayout mLlNoData;
    private int mPageSize = 10;
    private int mPage = 1;
    private boolean mIsFromPush;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        initView();
        initData(mPage);
    }

    private void initView() {
        mLlNoData = (LinearLayout) findViewById(R.id.ll_nodata);
        spUtil.putBoolean(mContext, "news", false);
        setTitle(getString(R.string.chat_jingbao_news));
        mRcvNews = (XRecyclerView) findViewById(R.id.rcv_news);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRcvNews.setLayoutManager(manager);
        mAdapter = new NewsAdapter(mContext);
        mRcvNews.setAdapter(mAdapter);
        mRcvNews.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPage = 1;
                initData(mPage);
            }

            @Override
            public void onLoadMore() {
                mPage++;
                initData(mPage);
            }
        });
        DialogUtil.showLoadDiadlog(mContext);
    }

    private void initData(final int page) {

        Intent intent = getIntent();
        if (intent != null) {
            mIsFromPush = intent.getBooleanExtra("isFromPush", false);
        }
        HashMap<String, String> param = new HashMap<>();
        param.put("token", mUser.getToken());
        param.put("pageRows", "" + mPageSize);
        param.put("curPage", "" + page);
        mManager.post(NetworkAPI.NewsList, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(mContext);
                List<NewsBean> list = GsonUtil.paraeFromStringToList(value, NewsBean.class);
                if (page == 1) {
                    mAdapter.clear();
                    if (list.size() == 0) {
                        mLlNoData.setVisibility(View.VISIBLE);
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLlNoData.getLayoutParams();
                        params.setMargins(0, (DeviceUtil.getHeight(mContext) - DeviceUtil.dip2px(mContext, 50)) / 10 * 4 - DeviceUtil.dip2px(mContext, 75), 0, 0);
                        mLlNoData.setLayoutParams(params);
                        mRcvNews.setVisibility(View.GONE);
                    } else {
                        mLlNoData.setVisibility(View.GONE);
                        mRcvNews.setVisibility(View.VISIBLE);
                    }
                }
                mAdapter.setData(list);
                mAdapter.notifyDataSetChanged();
                mRcvNews.loadMoreComplete();
                mRcvNews.refreshComplete();
                if (list.size() < mPageSize) {
                    mRcvNews.setNoMore(true);
                }
            }

            @Override
            public void onFailure(String value) {
                DialogUtil.dismiss(mContext);
                ToastOpt.createToast(mContext, "请求失败");
                mLlNoData.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLlNoData.getLayoutParams();
                params.setMargins(0, (DeviceUtil.getHeight(mContext) - DeviceUtil.dip2px(mContext, 50)) / 10 * 4 - DeviceUtil.dip2px(mContext, 75), 0, 0);
                mLlNoData.setLayoutParams(params);
                mRcvNews.setVisibility(View.GONE);

            }
        }, param);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mIsFromPush) {
            onBaClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mIsFromPush) {
                        startActivity(MainActivity.class, null);
                    }
                    finish();
                }
            });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mIsFromPush) {
            startActivity(MainActivity.class, null);
        }
        finish();
        return super.onKeyDown(keyCode, event);
    }
}
